package cn.itsource.crm.web.controller;

import cn.itsource.AjaxResult;
import cn.itsource.crm.domain.User;
import cn.itsource.crm.service.IUserService;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
@RequestMapping("/user")
@Api(tags = "用户接口类")
public class UserController {

    @Autowired
    private IUserService userService;

    /**
     *  put/delete/post/get/patch
     *
     */
    @RequestMapping(value = "/list",method = RequestMethod.GET)
    @ResponseBody
    @ApiResponses({
         @ApiResponse(code=200,message="成功"),
            @ApiResponse(code=500,message="服务器错误")
    })
    public List<User> queryAll(){
        System.out.println("patch请求");
        return userService.queryAll();
    }

    // /user/1
    @RequestMapping(value = "/{id}",method = RequestMethod.GET)
//    @ApiOperation(value="根据ID查询用户",notes = "getOne查询方法")
//    @ApiImplicitParams({
//            @ApiImplicitParam(name = "id",value = "id参数",required = true,dataType = "Long",paramType = "path")
//    })
    @ResponseBody
    public User getOne(@PathVariable Long id){
        System.out.println("id："+id);
        User user=new User();
        user.setId(1L);
        user.setName("刚刚");
        return user;
    }


    @RequestMapping(value = "/save",method = RequestMethod.PUT)
    @ResponseBody
    public AjaxResult save(@RequestBody  User user){
        try {
            System.out.println("接收的user对象:");
            System.out.println(user);
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult("保存失败");

        }
        return new AjaxResult();
    }
    //删除
    @RequestMapping(value = "{id}",method = RequestMethod.DELETE)
    @ResponseBody
    public AjaxResult delete(@PathVariable Long id){
        try {
            System.out.println("删除的id是:"+id);

        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult("删除失败");

        }
        return new AjaxResult();
    }

    //修改
    @RequestMapping(value = "/update",method = RequestMethod.POST)
    @ResponseBody
    public AjaxResult update(@RequestBody  User user){
        try {
            System.out.println("修的对象:"+user);
            System.out.println(user.getId());

        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult("修改失败");

        }
        return new AjaxResult();
    }
}
